package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {

    ////将xlsx表格上传到数据库
    @Override
    public void importOrderSettings(List<String[]> orderSettingList) {

        //将xlsx表格字符串数组集合转换为OrderSetting对象集合
        List<OrderSetting> orderSettingImportList = changeString2OrderSetting(orderSettingList);
        //遍历集合判断数据库是否存在对应日期数据
        for (OrderSetting orderSetting : orderSettingImportList) {
            //按照日期查询表格数据是否存在
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ORDERDATE", orderSetting.getOrderDate());
            OrderSetting orderSettingIsOrNotExist = baseMapper.selectOne(queryWrapper);
            //如果数据库中存在改表格数据，那么将主键id设置到该OrderSetting对象上去
            //以保证mp执行更新方法
            if (orderSettingIsOrNotExist != null) {
                orderSetting.setId(orderSettingIsOrNotExist.getId());
            }
        }
        saveOrUpdateBatch(orderSettingImportList);
    }



    //将xlsx表格字符串数组集合转换为OrderSetting对象集合
    private List<OrderSetting> changeString2OrderSetting(List<String[]> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            //创建集合接收OrderSetting对象
            List<OrderSetting> orderSettingImportList = new ArrayList<>();
            for (String[] string : orderSettingList) {
                //xlsx表格中没有主键，所以没有setId
                OrderSetting orderSetting = new OrderSetting();
                //已预约人数
                orderSetting.setReservations(0);
                //预约设置日期
                orderSetting.setOrderDate(DateUtils.parseString2Date(string[0],"yyyy/MM/dd"));
                //可预约人数
                orderSetting.setNumber(Integer.parseInt(string[1]));
                orderSettingImportList.add(orderSetting);
            }
            return orderSettingImportList;
        }
        return null;
    }



    //查询当前日期的预约数据
    @Override
    public Map findSettingData(int year, int month) {
        //月初
        String beginDate = year + "-" + month + "-1";
        //月末
        String endDate = year + "-" + month + "-31";
        //查询数据库
        List<OrderSetting> orderSettings = baseMapper.findSettingDataByYearAndMonth(beginDate, endDate);
        //创建Map集合用于将前端所需要的数据的封装
        HashMap<Object, Object> map = new HashMap<>();
        //遍历数据库查询的数据信息、
        for (OrderSetting orderSetting : orderSettings) {
            //获取日期信息
            Date orderDate = orderSetting.getOrderDate();
            //将日期转化为字符串格式
            String dateString = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
//            //方式1创建Vo：将用到的字段放入orderSetting中
//            OrderSetting orderSettingVo = new OrderSetting();
//            orderSettingVo.setNumber(orderSetting.getNumber());
//            orderSettingVo.setReservations(orderSetting.getReservations());
//            //放入map集合
//            map.put(dateString, orderSettingVo);
              //方式2创建Map
            HashMap orderSettingMap = new HashMap<>();
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservations", orderSetting.getReservations());
            //放入map集合
            map.put(dateString, orderSettingMap);
        }
        return map;
    }

    //更新当前日期的最大预约人数
    @Override
    public Boolean updateNumberByOrderdate(int number, String settingDay) {
        return baseMapper.updateNumberByOrderdate(number, settingDay);
    }

    //移动端效验是否已经预约满
    @Override
    public int isOrderOk(String orderDate) {
        int count = baseMapper.isOrderOk(orderDate);
        return count;
    }
    //移动端更新预约人数表,预约成功，预约人数+1
    @Override
    //开启分布式事务
    @LcnTransaction
    public void updateReservationByOrderDate(String orderDate) {
        baseMapper.updateReservationByOrderDate(orderDate);
    }


    //定时清理预约历史数据
    @Override
    //开启分布式事务
    @LcnTransaction
    public void xxiJobClearorderSetting() {
       //根据当前日期更新预约表中的预约人数
       //baseMapper.updateOrderSettingByCurdate();
       //根据当前日期删除垃圾预约表信息
        baseMapper.deleteOrderSettingByCurdate();
    }
}
